<%@page import="java.net.URLEncoder"%>
<%@page import="java.io.RandomAccessFile"%>
<%@page import="java.io.File"%>
<%@page import="java.io.PrintWriter"%>
<%@page import="java.io.StringWriter"%>
<%@page import="com.alibaba.fastjson.JSONObject"%>
<%@page import="weaver.general.Util"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
	JSONObject json = new JSONObject();
	String logPath = Util.null2String(request.getParameter("logPath"));
	long numRead = Util.getIntValue(request.getParameter("numRead"),500);
	int count = 0;  
	String textData = "";
	try{
		// 排除不可读状态  
		File file = new File(logPath);
	    if (!file.exists() || file.isDirectory() || !file.canRead())  {  
	    	textData = "当前文件不可以读！";
	    }else{
	    	// 使用随机读取  
	        RandomAccessFile fileRead = null;  
	    	try{
	    		//使用读模式  
	            fileRead = new RandomAccessFile(file, "r");  
	            //读取文件长度  
	            long length = fileRead.length();  
	            //如果是0，代表是空文件，直接返回空结果  
	            if (length == 0L){  
	            	textData = "文件无任何记录！";
	            }else{
	            	//初始化游标  
	                long pos = length - 1;  
	                while (pos > 0)  {  
	                    pos--;  
	                    //开始读取  
	                    fileRead.seek(pos);  
	                    //如果读取到\n代表是读取到一行  
	                    if (fileRead.readByte() == '\n')  {  
	                        //使用readLine获取当前行  
	                        String line = new String(fileRead.readLine().getBytes("ISO-8859-1"),"GBK");  
	                        textData += line+"\n";
	                        //行数统计，如果到达了numRead指定的行数，就跳出循环  
	                        count++;  
	                        if (count == numRead){  
	                            break;  
	                        }  
	                    }  
	                }  
	                if (pos == 0)  {  
	                    fileRead.seek(0);  
	                    textData += new String(fileRead.readLine().getBytes("ISO-8859-1"),"GBK")+"\n";
	                }              	
	            }
	    	}catch(Exception e){
	    		StringWriter sw = new StringWriter();
	            PrintWriter pw = new PrintWriter(sw);
	            e.printStackTrace(pw);
	            textData = sw.toString();
	    	}finally {  
	            if (fileRead != null){  
	                try  {  
	                    //关闭资源  
	                    fileRead.close();  
	                }catch (Exception e){}  
	            }  
	        }  
	    }
	}catch(Exception e){
		StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        textData = sw.toString();
	}
    out.print(textData);
%>